// https://blog.csdn.net/AS_Tammy/article/details/93601836

const symmetricalTree = {
    val: 8,
    left: {
        val: 6,
        left: { val: 5, left: null, right: null },
        right: { val: 7, left: null, right: null }
    },
    right: {
        val: 6,
        left: { val: 7, left: null, right: null },
        right: { val: 5, left: null, right: null }
    }
}

// 序列化 前序遍历
function serialize(root, result = []) {
    if (!root) { //  函数内处理为空的情况
        result.push("#")
        return
    };
    result.push(root.val)
    serialize(root.left, result) // 不能判断不执行 
    serialize(root.right, result)
    return result.join(",")
}
let se = serialize(symmetricalTree)
console.log(se)

// 反序列化
function Deseria(str) {
    if (!str) return null
    console.log(str)
    let arr = str.split(",")
    // console.log("arr前",arr)
    return deserialize(arr)
}
function deserialize(arr) {
    if(!arr.length) return
    let node = null;
    // console.log("arr",arr)
    let curr = arr.shift()
    if (curr !== "#") {
        node = { val: curr }
        node.left = deserialize(arr)
        node.right = deserialize(arr)
    }
    return node
}

console.log(Deseria(se))